<html>
<head>
	<title>Programming with SQL Relay using the C API</title>
	<link rel="stylesheet" href="../css/styles.css">
</head>
<body>
<h1>Programming with SQL Relay using the C API</h1>

<ul>
  <li><a href="#compiling">Compiling an SQL Relay Client Program</a></li>
  <li><a href="#session">Establishing a Session</a></li>
  <li><a href="#query">Executing Queries</a></li>
  <li><a href="#commit">Commits and Rollbacks</a></li>
  <li><a href="#temptables">Temporary Tables</a></li>
  <li><a href="#errors">Catching Errors</a></li>
  <li><a href="#bindvars">Substitution and Bind Variables</a></li>
  <li><a href="#rebinding">Re-Binding and Re-Executing</a></li>
  <li><a href="#fields">Accessing Fields in the Result Set</a></li>
  <li><a href="#largeresultsets">Dealing With Large Result Set</a></li>
  <li><a href="#cursors">Cursors</a></li>
  <li><a href="#columns">Getting Column Information</a></li>
  <li><a href="#storedprocedures">Stored Procedures</a></li>
  <li><a href="#caching">Caching The Result Set</a></li>
  <li><a href="#suspending">Suspending and Resuming Sessions</a></li>
  <li><a href="#lastinsertid">Getting the Last Insert ID</a></li>
</ul>

<br/><a name="compiling"/><h2>Compiling an SQL Relay Client Program</h2>

<p>When writing an SQL Relay client program using the C API, you need to
include the sqlrclientwrapper.h file.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
</tt></pre>

</blockquote>
<p>You'll also need to link against the sqlrclientwrapper, sqlrclient and 
rudiments libraries.</p>

<p>On Linux and Unix, the header files are usually found under
/usr/local/firstworks/include and the libraries are usually found in 
/usr/local/firstworks/lib.</p>

<p>On Windows, the header files are usually found under C:\Program Files\Firstworks\include, the .lib files are usually found under C:\Program Files\Firstworks\lib and the .dll files are usually found under C:\Program Files\Firstworks\bin.</p>

<p>The command to compile your .c file to object code will look something like
this (assuming you're using the GNU C compiler on Linux or Unix):</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>gcc <font color="#990000">-</font>I<font color="#990000">/</font>usr<font color="#990000">/</font>local<font color="#990000">/</font>firstworks<font color="#990000">/</font>include <font color="#990000">-</font><font color="#008080">c</font> myprogram<font color="#990000">.</font>c</tt></pre>

</blockquote>
<p>The command to compile your .o file to an executable will look something like
this (assuming you're using the GNU C++ compiler on Linux or Unix):</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>g<font color="#990000">++</font> <font color="#990000">-</font>o <font color="#008080">myprogram</font> myprogram<font color="#990000">.</font>o <font color="#990000">-</font>L<font color="#990000">/</font>usr<font color="#990000">/</font>local<font color="#990000">/</font>firstworks<font color="#990000">/</font>lib <font color="#990000">-</font>lsqlrclientwrapper <font color="#990000">-</font>lsqlrclient <font color="#990000">-</font>lrudiments</tt></pre>

</blockquote>
<p>Note that g++ was used to link, not gcc.  You could alternatively link using
gcc like this:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>gcc <font color="#990000">-</font>o <font color="#008080">myprogram</font> myprogram<font color="#990000">.</font>o <font color="#990000">-</font>L<font color="#990000">/</font>usr<font color="#990000">/</font>local<font color="#990000">/</font>firstworks<font color="#990000">/</font>lib <font color="#990000">-</font>lsqlrclientwrapper <font color="#990000">-</font>lsqlrclient <font color="#990000">-</font>lrudiments <font color="#990000">-</font>lstdc<font color="#990000">++</font></tt></pre>

</blockquote>
<p>When using the C API, it is important to compile the .c files to object code
using the C compiler before linking them using the C++ compiler.  
Compiling/Linking in one step using the C++ compiler the will most likely fail
as it will generate C++ style symbols for function calls which will not be 
resolved in the sqlrclientwrapper library since it contains C style function 
symbols.  Compiling to object code using the C compiler as a seperate step
ensures that C style symbols will be generated for function calls.</p>

<a name="session"/><h2>Establishing a Session</h2>

<p>To use SQL Relay, you have to identify the connection that you intend to 
use.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>

        <font color="#990000">...</font> execute <font color="#008080">some</font> queries <font color="#990000">...</font>

        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>After calling the constructor, a session is established when the first 
query is run.</p>

<p>For the duration of the session, the client occupies one of the database
connections, so care should be taken to minimize the length of a
session.</p>

<p>Sessions can also be encrypted and authenticated using Kerberos/Active Directory or TLS/SSL.  The sqlrcon_enableKerberos() function is provided to enable Kerberos/Active Directory encryption and authentication and the sqlrcon_enableTls() function is provided to enable TLS/SSL encryption and authentication.  The sqlrcon_disableEncryption() function is provided to disable any previously enabled encryption.</p>

<p>See the <a href="../api/c/html/group_sqlrclientwrapper.html">C API reference</a> for information about these functions and the SQL Relay Configuration Guide for more information about <a href="../admin/configguide.html#krb">Kerberos/Active Directory</a> and <a href="../admin/configguide.html#tls">TLS/SSL</a> configurations.  In particular, note that user and password are not typically used when using Kerberos/AD.</p>

<a name="query"/><h2>Executing Queries</h2>

<p>Call sqlrcur_sendQuery() or sqlrcur_sendFileQuery() to run a query.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_table"</font><font color="#990000">);</font>

        <font color="#990000">...</font> <b><font color="#0000FF">do</font></b> some stuff that takes a <font color="#008080">short</font> time <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_sendFileQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"/usr/local/myprogram/sql"</font><font color="#990000">,</font><font color="#FF0000">"myquery.sql"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <font color="#990000">...</font> <b><font color="#0000FF">do</font></b> some stuff that takes a <font color="#008080">long</font> time <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_other_table"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <font color="#990000">...</font> process the <font color="#008080">result</font> set <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>Note the call to sqlrcon_endSession() after the call to sqlrcur_sendFileQuery().
Since the program does some stuff that takes a long time between that query and
the next, ending the session there allows another client an opportunity to use 
that database connection while your client is busy.  The next call to 
sqlrcur_sendQuery() establishes another session.  Since the program does some 
stuff that takes a short time between the first two queries, it's OK to leave 
the session open between them.</p>

<a name="commit"/><h2>Commits and Rollbacks</h2>

<p>If you need to execute a commit or rollback, you should use the 
sqlrcon_commit() and sqlrcon_rollback() functions rather than sending a "commit"
or "rollback" query.  There are two reasons for this.  First, it's much more 
efficient to call the functions.  Second, if you're writing code that can run on 
transactional or non-transactional databases, some non-transactional databases 
will throw errors if they receive a "commit" or "rollback" query, but by calling
the sqlrcon_commit() and sqlrcon_rollback() functions you instruct the database
connection daemon to call the commit and rollback API functions for that database
rather than issuing them as queries.  If the API's have no commit or rollback 
functions, the calls do nothing and the database throws no error.</p>

<p>You can also turn Autocommit on or off with the sqlrcon_autoCommitOn() and 
sqlrcon_autoCommitOff() functions.  When Autocommit is on, the database 
performs a commit after each successful DML or DDL query.  When Autocommit is 
off, the database commits when the client instructs it to, or (by default) when
a client disconnects.  For databases that don't support Autocommit, 
sqlrcon_autoCommitOn() and sqlrcon_autoCommitOff() have no effect.</p>

<a name="temptables"/><h2>Temporary Tables</h2>

<p>Some databases support temporary tables.  That is, tables which are
automatically dropped or truncated when an application closes its connection
to the database or when a transaction is committed or rolled back.</p>

<p>For databases which drop or truncate tables when a transaction is committed
or rolled back, temporary tables work naturally.</p>

<p>However, for databases which drop or truncate tables when an application
closes its connection to the database, there is an issue.  Since SQL Relay
maintains persistent database connections, when an application disconnects from
SQL Relay, the connection between SQL Relay and the database remains, so
the database does not know to drop or truncate the table.  To remedy this
situation, SQL Relay parses each query to see if it created a temporary table,
keeps a list of temporary tables and drops (or truncates them) when the
application disconnects from SQL Relay.  Since each database has slightly
different syntax for creating a temporary table, SQL Relay parses each query
according to the rules for that database.</p>

<p>In effect, temporary tables should work when an application connects to
SQL Relay in the same manner that they would work if the application connected
directly to the database.</p>

<a name="errors"/><h2>Catching Errors</h2>

<p>If your call to sqlrcur_sendQuery() or sqlrcur_sendFileQuery() returns a 0, the 
query failed.  You can find out why by calling sqlrcur_errorMessage().</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#0000FF">if</font></b> <font color="#990000">(!</font><b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_nonexistant_table"</font><font color="#990000">))</font> <font color="#FF0000">{</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"%s</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_errorMessage</font></b><font color="#990000">(</font>cur<font color="#990000">));</font>
        <font color="#FF0000">}</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<br/><a name="bindvars"/><h2>Substitution and Bind Variables</h2>

<p>Programs rarely execute fixed queries.  More often than not, some part
of the query is dynamically generated.  The SQL Relay API provides functions for
making substitutions and binds in those queries.</p>

<p>For a detailed discussion of substitutions and binds, see
<a href="binds.html">this document</a>.</p>

<p>Rather than just calling sendQuery() you call prepareQuery(),
substitution(), inputBind() and executeQuery().  If you using queries
stored in a file, you can call prepareFileQuery() instead of prepareQuery().</p>

<p>When passing a floating point number in as a bind or substitution variable,
you have to supply precision and scale for the number.  See
<a href="precisionscale.html">this page</a> for a discussion of precision and
scale.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from mytable $(whereclause)"</font><font color="#990000">)</font>
        <b><font color="#000000">sqlrcur_substitution</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"whereclause"</font><font color="#990000">,</font><font color="#FF0000">"where stringcol=:stringval and integercol&gt;:integerval and floatcol&gt;floatval"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBind</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"stringval"</font><font color="#990000">,</font><font color="#FF0000">"true"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBind</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"integerval"</font><font color="#990000">,</font><font color="#993399">10</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBind</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"floatval"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>

        <font color="#990000">...</font> process the <font color="#008080">result</font> set <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>If you are curious how many bind variables have been declared in a query,
you can call countBindVariables() after preparing the query.</p>

<p>If you're using a database with an embedded procedural language, you may
want to retrieve data from function calls.  To facilitate this, SQL Relay
provides functions for defining and retrieving output bind variables.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"begin  :result1:=addTwoIntegers(:integer1,:integer2);  :result2=addTwoFloats(:float1,:float2);  :result3=convertToString(:integer3); end;"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBind</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"integer1"</font><font color="#990000">,</font><font color="#993399">10</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBind</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"integer2"</font><font color="#990000">,</font><font color="#993399">20</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBind</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"float1"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBind</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"float2"</font><font color="#990000">,</font><font color="#993399">2.2</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBind</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"integer3"</font><font color="#990000">,</font><font color="#993399">30</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_defineOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"result1"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_defineOutputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"result2"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_defineOutputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"result3"</font><font color="#990000">,</font><font color="#993399">100</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <font color="#008080">int64_t</font>    result1<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"result1"</font><font color="#990000">);</font>
        <font color="#009900">double</font>     result2<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"result2"</font><font color="#990000">);</font>
        <font color="#009900">char</font>       <font color="#990000">*</font>result3<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"result3"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <font color="#990000">...</font> <b><font color="#0000FF">do</font></b> something with <font color="#008080">the</font> result <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>The getOutputBindString() function returns a NULL value as an empty string.  
If you would it to come back as a NULL instead, you can call the 
getNullsAsNulls() function.  To revert to the default behavior, you can
call getNullsAsEmptyStrings().</p>

<p>You can insert data into BLOB and CLOB columns using the inputBindBlob(), inputBindClob() functions.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"create table images (image blob, description clob)"</font><font color="#990000">);</font>

        <font color="#009900">unsigned</font> <font color="#009900">char</font>   imagedata<font color="#990000">[</font><font color="#993399">40000</font><font color="#990000">];</font>
        <font color="#009900">unsigned</font> <font color="#009900">long</font>   imagelength<font color="#990000">;</font>

        <font color="#990000">...</font> read an image from a file into imagedata and the length of the
                file <font color="#008080">into</font> imagelength <font color="#990000">...</font>

        <font color="#009900">unsigned</font> <font color="#009900">char</font>   description<font color="#990000">[</font><font color="#993399">40000</font><font color="#990000">];</font>
        <font color="#009900">unsigned</font> <font color="#009900">long</font>   desclength<font color="#990000">;</font>

        <font color="#990000">...</font> read a description from a file into description and the length of
                the file <font color="#008080">into</font> desclength <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"insert into images values (:image,:desc)"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBindBlob</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"image"</font><font color="#990000">,</font>imagedata<font color="#990000">,</font>imagelength<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBindClob</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"desc"</font><font color="#990000">,</font>description<font color="#990000">,</font>desclength<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>Likewise, you can retreive BLOB or CLOB data using
defineOutputBindBlob()/getOutputBindBlob() and
defineOutputBindClob()/getOutputBindClob().</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"begin  select image into :image from images;  select description into :desc from images;  end;"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_defineOutputBindBlob</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"image"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_defineOutputBindClob</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"desc"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>

        <font color="#009900">char</font>    <font color="#990000">*</font>image<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindBlob</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"image"</font><font color="#990000">);</font>
        <font color="#009900">long</font>    imagelength<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindLength</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"image"</font><font color="#990000">);</font>

        <font color="#009900">char</font>    <font color="#990000">*</font>desc<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindClob</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"desc"</font><font color="#990000">);</font>
        <font color="#009900">char</font>    <font color="#990000">*</font>desclength<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindLength</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"desc"</font><font color="#990000">);</font>

        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <font color="#990000">...</font> <b><font color="#0000FF">do</font></b> something with image <font color="#008080">and</font> desc <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>Sometimes it's convenient to bind a bunch of variables that may or may not 
actually be in the query.  For example, if you are building a web based
application, it may be easy to just bind all the form variables/values from the
previous page, even though some of them don't appear in the query.  Databases
usually generate errors in this case.  Calling validateBinds() just prior to
calling executeQuery() causes the API to check the query for each bind variable
before actually binding it, preventing those kinds of errors.   You can also
call validBind() to see if a specific variable is valid.  However there is
a performance cost associated with calling validateBinds() and validBind().</p>

<a name="rebinding"/><h2>Re-Binding and Re-Execution</h2>

<p>Another feature of the prepare/bind/execute paradigm is the ability to 
prepare, bind and execute a query once, then re-bind and re-execute the query 
over and over without re-preparing it.  If your backend database natively 
supports this paradigm, you can reap a substantial performance improvement.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from mytable where mycolumn&gt;:value"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"value"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>

        <font color="#990000">...</font> process the <font color="#008080">result</font> set <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_clearBinds</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"value"</font><font color="#990000">,</font><font color="#993399">5</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>

        <font color="#990000">...</font> process the <font color="#008080">result</font> set <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_clearBinds</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"value"</font><font color="#990000">,</font><font color="#993399">10</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>

        <font color="#990000">...</font> process the <font color="#008080">result</font> set <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<br/><a name="fields"/><h2>Accessing Fields in the Result Set</h2>

<p>The sqlrcur_rowCount(), sqlrcur_colCount(), sqlrcur_getFieldByName() and
sqlrcur_getFieldByIndex() functions are useful for processing result sets.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#009900">int</font>        row<font color="#990000">,</font>col<font color="#990000">;</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_table"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>row<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> row<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_rowCount</font></b><font color="#990000">(</font>cur<font color="#990000">);</font> row<font color="#990000">++)</font> <font color="#FF0000">{</font>
                <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>col<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> col<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_colCount</font></b><font color="#990000">(</font>cur<font color="#990000">);</font> col<font color="#990000">++)</font> <font color="#FF0000">{</font>
                        <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"%s,"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>row<font color="#990000">,</font>col<font color="#990000">));</font>
                <font color="#FF0000">}</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">);</font>
        <font color="#FF0000">}</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>The sqlrcur_getFieldByName() and sqlrcur_getFieldByIndex() functions return
strings.  If you would like to get a field as a long or double, you can use
sqlrcur_getFieldAsLongByName()/sqlrcur_getFieldAsLongByIndex() and
sqlrcur_getFieldAsDoubleByName()/sqlrcur_getFieldAsDoubleByIndex().</p>

<p>You can also use sqlrcur_getRow() which returns a NULL-terminated array of
the fields in the row.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#009900">int</font>        row<font color="#990000">,</font>col<font color="#990000">;</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_table"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>row<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> row<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_rowCount</font></b><font color="#990000">(</font>cur<font color="#990000">);</font> row<font color="#990000">++)</font> <font color="#FF0000">{</font>
                <font color="#009900">char</font>    <font color="#990000">**</font>rowarray<font color="#990000">=</font><b><font color="#000000">sqlrcur_getRow</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>row<font color="#990000">);</font>
                <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>col<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> col<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_colCount</font></b><font color="#990000">(</font>cur<font color="#990000">);</font> col<font color="#990000">++)</font> <font color="#FF0000">{</font>
                        <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"%s,"</font><font color="#990000">,</font>rowarray<font color="#990000">[</font>col<font color="#990000">]);</font>
                <font color="#FF0000">}</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">);</font>
        <font color="#FF0000">}</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>The sqlrcur_getFieldByIndex(), sqlrcur_getFieldByName() and sqlrcur_getRow() 
functions return NULL fields as empty strings.  If you would like them to come 
back as NULL's instead, you can call the sqlrcur_getNullsAsNulls() function.  To 
revert to the default behavior, you can call sqlrcur_getNullsAsEmptyStrings().</p>

<p>If you want to access the result set, but don't care about the
column information (column names, types or sizes) and don't mind getting
fields by their numeric index instead of by name,  you can call the
sqlrcur_dontGetColumnInfo() function prior to executing your query.  This can 
result in a performance improvement, especially when many queries with small
result sets are executed in rapid succession.  You can call 
sqlrcur_getColumnInfo() again later to turn off this feature.</p>

<a name="largeresultsets"/><h2>Dealing With Large Result Sets</h2>

<p>SQL Relay normally buffers the entire result set.  This can speed things up 
at the cost of memory.  With large enough result sets, it makes sense to buffer the result set in chunks instead of all at once.</p>

<p>Use sqlrcur_setResultSetBufferSize() to set the number of rows to buffer at a 
time.  Calls to sqlrcur_getRow(), sqlrcur_getFieldByIndex() and 
sqlrcur_getFieldByName() cause the chunk containing the requested field to be 
fetched.  Rows in that chunk are accessible but rows before it are not.</p>

<p>For example, if you setResultSetBufferSize(5) and execute a query that 
returns 20 rows, rows 0-4 are available at once, then rows 5-9, then 10-14, 
then 15-19.  When rows 5-9 are available, getFieldByIndex(0,0) will return NULL
and getFieldByIndex(11,0) will cause rows 10-14 to be fetched and return the 
requested value.</p>

<p>When buffering the result set in chunks, don't end the session until after
you're done with the result set.</p>

<p>If you call sqlrcur_setResultSetBufferSize() and forget what you set it to, 
you can always call sqlrcur_getResultSetBufferSize().</p>

<p>When buffering a result set in chunks, the sqlrcur_rowCount() function returns
the number of rows returned so far.  The sqlrcur_firstRowIndex() function returns 
the index of the first row of the currently buffered chunk.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#009900">int</font>     done<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font>
        <font color="#009900">int</font>     row<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font>
        <font color="#009900">int</font>     col<font color="#990000">;</font>
        <font color="#009900">char</font>    <font color="#990000">*</font>field<font color="#990000">;</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_setResultSetBufferSize</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">5</font><font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_table"</font><font color="#990000">);</font>

        <b><font color="#0000FF">while</font></b> <font color="#990000">(!</font>done<font color="#990000">)</font> <font color="#FF0000">{</font>
                <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>col<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> col<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_colCount</font></b><font color="#990000">(</font>cur<font color="#990000">);</font> col<font color="#990000">++)</font> <font color="#FF0000">{</font>
                        <b><font color="#0000FF">if</font></b> <font color="#990000">(</font>field<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>row<font color="#990000">,</font>col<font color="#990000">))</font> <font color="#FF0000">{</font>
                                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"%s,"</font><font color="#990000">,</font>field<font color="#990000">);</font>
                        <font color="#FF0000">}</font> <b><font color="#0000FF">else</font></b> <font color="#FF0000">{</font>
                                done<font color="#990000">=</font><font color="#993399">1</font><font color="#990000">;</font>
                        <font color="#FF0000">}</font>
                <font color="#FF0000">}</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">);</font>
                row<font color="#990000">++;</font>
        <font color="#FF0000">}</font>

        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_other_table"</font><font color="#990000">);</font>

        <font color="#990000">...</font> process <font color="#008080">this</font> query<font color="#FF0000">'s result set in chunks also ...</font>

<font color="#FF0000">        sqlrcur_setResultSetBufferSize(cur,0);</font>

<font color="#FF0000">        sqlrcur_sendQuery(cur,"select * from my_third_table");</font>

<font color="#FF0000">        ... process this query'</font>s result set all <font color="#008080">at</font> once <font color="#990000">...</font>

        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<a name="cursors"/><h2>Cursors</h2>

<p>Cursors make it possible to execute queries while processing the result
set of another query.  You can select rows from a table in one query, then 
iterate through its result set, inserting rows into another table, using only 
1 database connection for both operations.</p>

<p>For example:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">;</font>
        <font color="#008080">sqlrcur</font>      cursor1<font color="#990000">;</font>
        <font color="#008080">sqlrcur</font>      cursor2<font color="#990000">;</font>
        <font color="#009900">int</font>          index<font color="#990000">;</font>

        con<font color="#990000">=</font><b><font color="#0000FF">new</font></b> <b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        cursor1<font color="#990000">=</font><b><font color="#0000FF">new</font></b> <b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
        cursor2<font color="#990000">=</font><b><font color="#0000FF">new</font></b> <b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_setResultSetBufferSize</font></b><font color="#990000">(</font>cursor1<font color="#990000">,</font><font color="#993399">10</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cursor1<font color="#990000">,</font><font color="#FF0000">"select * from my_huge_table"</font><font color="#990000">);</font>

        index<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font>
        <b><font color="#0000FF">while</font></b> <font color="#990000">(!</font><b><font color="#000000">sqlrcur_endOfResultSet</font></b><font color="#990000">(</font>cursor1<font color="#990000">))</font> <font color="#FF0000">{</font>
                <b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cursor2<font color="#990000">,</font><font color="#FF0000">"insert into my_other_table values (:1,:2,:3)"</font><font color="#990000">);</font>
                <b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cursor2<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cursor1<font color="#990000">,</font>index<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">));</font>
                <b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cursor2<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cursor1<font color="#990000">,</font>index<font color="#990000">,</font><font color="#993399">2</font><font color="#990000">));</font>
                <b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cursor2<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cursor1<font color="#990000">,</font>index<font color="#990000">,</font><font color="#993399">3</font><font color="#990000">));</font>
                <b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cursor2<font color="#990000">);</font>
        <font color="#FF0000">}</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cursor2<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cursor1<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>If you are using stored procedures with Oracle, a stored
procedure can execute a query and return a cursor.  A cursor bind variable can
then retrieve that cursor.  Your program can retrieve the result set from the
cursor.  All of this can be accomplished using defineOutputBindCursor(),
getOutputBindCursor() and fetchFromOutputBindCursor().</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#009900">int</font>        i<font color="#990000">,</font>j<font color="#990000">;</font>

        <font color="#008080">sqlrcon</font>         con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>         cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"begin  :curs:=sp_mytable; end;"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_defineOutputBindCursor</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"curs"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>

        <font color="#008080">sqlrcur</font>         bindcur<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindCursor</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"curs"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_fetchFromBindCursor</font></b><font color="#990000">(</font>bindcur<font color="#990000">);</font>

        <i><font color="#9A1900">// print fields from table</font></i>
        <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>i<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> i<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_rowCount</font></b><font color="#990000">(</font>bindcur<font color="#990000">);</font> i<font color="#990000">++)</font> <font color="#FF0000">{</font>
                <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>j<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> j<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_colCount</font></b><font color="#990000">(</font>bindcur<font color="#990000">);</font> j<font color="#990000">++)</font> <font color="#FF0000">{</font>
                        <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"%s,"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>bindcur<font color="#990000">,</font>i<font color="#990000">,</font>j<font color="#990000">));</font>
                <font color="#FF0000">}</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">);</font>
        <font color="#FF0000">}</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>bindcur<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>The number of cursors simultaneously available per-connection is set at 
compile time and defaults to 5.</p>

<a name="columns"/><h2>Getting Column Information</h2>

<p>For each column, the API supports getting the name, type and length of each
field.  All databases support these attributes.  The API also supports
getting the precision, scale (see <a href="precisionscale.html">this page</a>
for a discussion of precision and scale), length of the longest field, and
whether the
column is nullable, the primary key, unique, part of a key, unsigned,
zero-filled, binary, or an auto-incrementing field.  However, not all databases
support these attributes.  If a database doesn't support an attribute, it is
always returned as false.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#009900">int</font>        i<font color="#990000">;</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_table"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>i<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> i<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_colCount</font></b><font color="#990000">(</font>cur<font color="#990000">);</font> i<font color="#990000">++)</font> <font color="#FF0000">{</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Name:          %s</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnName</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Type:          %s</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnType</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Length:        %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnLength</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Precision:     %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnPrecision</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Scale:         %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnScale</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Longest Field: %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getLongest</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Nullable:      %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnIsNullable</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Primary Key:   %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnIsPrimaryKey</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Unique:        %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnIsUnique</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Part of Key:   %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnIsPartOfKey</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Unsigned:      %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnIsUnsigned</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Zero Filled:   %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnIsZeroFilled</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Binary:        %d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnIsBinary</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Auth Increment:%d</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnIsAutoIncrement</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">);</font>
        <font color="#FF0000">}</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>Some databases force column names to upper case, others force column names
to lower case, and others still support mixed-case column names.  Sometimes,
when migrating between databases, you can run into trouble.  You can use
upperCaseColumnNames() and lowerCaseColumnNames() to cause column names to be
converted to upper or lower case, or you can use mixedCaseColumnNames() to
cause column names to be returned in the same case as they are defined in the
database.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#009900">int</font>        i<font color="#990000">;</font>

        <font color="#008080">sqlrcon</font>         con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>         cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <i><font color="#9A1900">// column names will be forced to upper case</font></i>
        <b><font color="#000000">sqlrcur_upperCaseColumnNames</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_endQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_table"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>i<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> i<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_colCount</font></b><font color="#990000">(</font>cur<font color="#990000">);</font> i<font color="#990000">++)</font> <font color="#FF0000">{</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Name:   %s</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnName</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
        <font color="#FF0000">}</font>

        <i><font color="#9A1900">// column names will be forced to lower case</font></i>
        <b><font color="#000000">sqlrcur_lowerCaseColumnNames</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_endQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_table"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>i<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> i<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_colCount</font></b><font color="#990000">(</font>cur<font color="#990000">);</font> i<font color="#990000">++)</font> <font color="#FF0000">{</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Name:   %s</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnName</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
        <font color="#FF0000">}</font>

        <i><font color="#9A1900">// column names will be the same as they are in the database</font></i>
        <b><font color="#000000">sqlrcur_mixedCaseColumnNames</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_endQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_table"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>i<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> i<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_colCount</font></b><font color="#990000">(</font>cur<font color="#990000">);</font> i<font color="#990000">++)</font> <font color="#FF0000">{</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"Name:   %s</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getColumnName</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>i<font color="#990000">));</font>
        <font color="#FF0000">}</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<br/><a name="storedprocedures"/><h2>Stored Procedures</h2>

<p>Many databases support stored procedures.  Stored procedures are sets of
queries and procedural code that are executed inside of the database itself.
For example, a stored procedure may select rows from one table, iterate through
the result set and, based on the values in each row, insert, update or delete
rows in other tables.  A client program could do this as well, but a stored
procedure is generally more efficient because queries and result sets don't
have to be sent back and forth between the client and database.  Also, stored
procedures are generally stored in the database in a compiled state, while
queries may have to be re-parsed and re-compiled each time they are sent.</p>

<p>While many databases support stored procedures.  The syntax for creating
and executing stored procedures varies greatly between databases.</p>

<p>Stored procedures typically take input paramters from client programs through
input bind variables and return values back to client programs either through
bind variables or result sets.  Stored procedures can be broken down into
several categories, based on the values that they return.  Some stored
procedures don't return any values, some return a single value, some return
multiple values and some return entire result sets.</p>

<h3>No Values</h3>

<p>Some stored procedures don't return any values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in1 <font color="#008080">in</font> number<font color="#990000">,</font> in2 <font color="#008080">in</font> number<font color="#990000">,</font> in3 <font color="#008080">in</font> varchar2<font color="#990000">)</font> is
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"begin exampleproc(:in1,:in2,:in3); end;"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>Sybase and Microsoft SQL Server</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> exampleproc @<font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> @<font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> @<font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">)</font> as
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>@in1<font color="#990000">,</font>@in2<font color="#990000">,</font>@in3<font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"exec exampleproc"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<h4>Firebird</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font><font color="#008080">in1</font> integer<font color="#990000">,</font> <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> as
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
        suspend<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"execute procedure exampleproc ?, ?, ?"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<h4>DB2</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">double</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> language sql
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"call exampleproc(?,?,?)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#008080">void</font> as <font color="#FF0000">'</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        insert into mytable values ($1,$2,$3);</font>
<font color="#FF0000">        return;</font>
<font color="#FF0000">end;'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select examplefunc($1,$2,$3)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
</tt></pre>

</blockquote>
<h4>MySQL/MariaDB</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"call exampleproc(?,?,?)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h3>Single Values</h3>

<p>Some stored procedures return single values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>In Oracle, stored procedures can return values through output parameters or
as return values of the procedure itself.</p>

<p>Here is an example where the procedure itself returns a value.  Note that
Oracle calls these functions.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in1 <font color="#008080">in</font> number<font color="#990000">,</font> in2 <font color="#008080">in</font> number<font color="#990000">,</font> in3 <font color="#008080">in</font> varchar2<font color="#990000">)</font> returns number is
begin
        <b><font color="#0000FF">return</font></b> in1<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select exampleproc(:in1,:in2,:in3) from dual"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>result<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<p>Here is an example where the value is returned through an output
parameter.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in1 <font color="#008080">in</font> number<font color="#990000">,</font> in2 <font color="#008080">in</font> number<font color="#990000">,</font> in3 <font color="#008080">in</font> varchar2<font color="#990000">,</font> out1 <font color="#008080">out</font> number<font color="#990000">)</font> as
begin
        out1<font color="#990000">:=</font>in1<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"begin exampleproc(:in1,:in2,:in3,:out1); end;"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out1"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#008080">int64_t</font>    result<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out1"</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>Sybase and Microsoft SQL Server</h4>

<p>In Sybase and Microsoft SQL Server, stored procedures return values
through output parameters rather than as return values of the procedure
itself.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> exampleproc @<font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> @<font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> @<font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font> @out1 <font color="#009900">int</font> output as
        select @out1<font color="#990000">=</font><b><font color="#000000">convert</font></b><font color="#990000">(</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font>@in1<font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"exec exampleproc"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out1"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#008080">int64_t</font>    result<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out1"</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>Firebird</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font><font color="#008080">in1</font> integer<font color="#990000">,</font> <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> <b><font color="#000000">returns</font></b> <font color="#990000">(</font><font color="#008080">out1</font> integer<font color="#990000">)</font> as
begin
        out1<font color="#990000">=</font>in1<font color="#990000">;</font>
        suspend<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from exampleproc(?,?,?)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>result<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>Alternatively, you can run a query like the following and receive the result
using an output bind variable.  Note that in Firebird, input and
output bind variable indices are distict from one another.  The index of the
output bind variable is 1 rather than 4, even though there were 3 input bind
variables.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"execute procedure exampleproc ?, ?, ?"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#008080">int64_t</font>    result<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>DB2</h4>

<p>In DB2, stored procedures return values through output parameters rather
than as return values of the procedure itself.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">double</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font> out <font color="#008080">out1</font> <font color="#009900">int</font><font color="#990000">)</font> language sql
begin
        <font color="#008080">set</font> out1 <font color="#990000">=</font> in1<font color="#990000">;</font>
end
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"call exampleproc(?,?,?,?)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"4"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#008080">int64_t</font>    result<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"4"</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#008080">int</font> as <font color="#FF0000">'</font>
<font color="#FF0000">declare</font>
<font color="#FF0000">        in1 int;</font>
<font color="#FF0000">        in2 float;</font>
<font color="#FF0000">        in3 char(20);</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        in1:=$1;</font>
<font color="#FF0000">        return;</font>
<font color="#FF0000">end;</font>
<font color="#FF0000">'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from examplefunc(1,2,3)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">4</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>result<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
</tt></pre>

</blockquote>
<h4>MySQL/MariaDB</h4>

<p>A single value can be returned from a MySQL/MariaDB function.</p>

<p>To create the function, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#009900">int</font> <b><font color="#0000FF">return</font></b> in1<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the function from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select examplefunc(?,?,?)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">4</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>result<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the function, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<p>A single value can be returned in the result set of a MySQL/MariaDB procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">()</font> begin select <font color="#993399">1</font><font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execeute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select exampleproc()"</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>result<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<p>A single value can be returned using the output variable of a MySQL/MariaDB
procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>out <font color="#008080">out1</font> <font color="#009900">int</font><font color="#990000">)</font> begin select <font color="#993399">1</font> <font color="#008080">into</font> out1<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execeute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"set @out1=0"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"call exampleproc(@out1)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select @out1"</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>result<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h3>Multiple Values</h3>

<p>Some stored procedures return multiple values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>In Oracle, stored procedures can return values through output parameters or
as return values of the procedure itself.  If a procedure needs to return
multiple values, it can return one of them as the return value of the procedure
itself, but the rest must be returned through output parameters.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in1 <font color="#008080">in</font> number<font color="#990000">,</font> in2 <font color="#008080">in</font> number<font color="#990000">,</font> in3 <font color="#008080">in</font> varchar2<font color="#990000">,</font> out1 <font color="#008080">out</font> number<font color="#990000">,</font> out2 <font color="#008080">out</font> number<font color="#990000">,</font> out3 <font color="#008080">out</font> varchar2<font color="#990000">)</font> is
begin
        out1<font color="#990000">:=</font>in1<font color="#990000">;</font>
        out2<font color="#990000">:=</font>in2<font color="#990000">;</font>
        out3<font color="#990000">:=</font>in3<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"begin exampleproc(:in1,:in2,:in3,:out1,:out2,:out3); end;"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out1"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out2"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out3"</font><font color="#990000">,</font><font color="#993399">20</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#008080">int64_t</font> out1<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out1"</font><font color="#990000">);</font>
<font color="#009900">double</font>  out2<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out2"</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out3<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out3"</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>Sybase and Microsoft SQL Server</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> exampleproc @<font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> @<font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> @<font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font> @out1 <font color="#008080">int</font> output<font color="#990000">,</font> @out2 <font color="#008080">int</font> output<font color="#990000">,</font> @out3 <font color="#009900">int</font> output as
        select @out1<font color="#990000">=</font><b><font color="#000000">convert</font></b><font color="#990000">(</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font>@in1<font color="#990000">),</font>
                @out2<font color="#990000">=</font><b><font color="#000000">convert</font></b><font color="#990000">(</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font>@in2<font color="#990000">),</font>
                @out2<font color="#990000">=</font><b><font color="#000000">convert</font></b><font color="#990000">(</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font>@in2<font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"exec exampleproc"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out1"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out2"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out3"</font><font color="#990000">,</font><font color="#993399">20</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#008080">int64_t</font> out1<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out1"</font><font color="#990000">);</font>
<font color="#009900">double</font>  out2<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out2"</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out3<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"out3"</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>Firebird</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font><font color="#008080">in1</font> integer<font color="#990000">,</font> <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> <b><font color="#000000">returns</font></b> <font color="#990000">(</font><font color="#008080">out1</font> integer<font color="#990000">,</font> <font color="#008080">out2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">out3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> as
begin
        out1<font color="#990000">=</font>in1<font color="#990000">;</font>
        out2<font color="#990000">=</font>in2<font color="#990000">;</font>
        out3<font color="#990000">=</font>in3<font color="#990000">;</font>
        suspend<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from exampleproc(?,?,?)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out1<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out2<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out3<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>Alternatively, you can run a query like the following and receive the result
using a output bind variables.  Note that in Firebird, input and
output bind variable indices are distict from one another.  The index of the
first output bind variable is 1 rather than 4, even though there were 3 input
bind variables.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"execute procedure exampleproc ?, ?, ?"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#993399">20</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#008080">int64_t</font> out1<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">);</font>
<font color="#009900">double</font>  out2<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out3<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>DB2</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">double</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font> out <font color="#008080">out1</font> <font color="#009900">int</font><font color="#990000">,</font> out <font color="#008080">out2</font> <font color="#009900">double</font><font color="#990000">,</font> out <font color="#008080">out3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> language sql
begin
        <font color="#008080">set</font> out1 <font color="#990000">=</font> in1<font color="#990000">;</font>
        <font color="#008080">set</font> out2 <font color="#990000">=</font> in2<font color="#990000">;</font>
        <font color="#008080">set</font> out3 <font color="#990000">=</font> in3<font color="#990000">;</font>
end
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"call exampleproc(?,?,?,?,?,?)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"4"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"5"</font><font color="#990000">);</font>
sqlrcur_defineOutputBindString<font color="#990000">-(</font>cur<font color="#990000">,</font><font color="#FF0000">"6"</font><font color="#990000">,</font><font color="#993399">25</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#008080">int64_t</font> out1<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindInteger</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"4"</font><font color="#990000">);</font>
<font color="#009900">double</font>  out2<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"5"</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out3<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"6"</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#008080">record</font> as <font color="#FF0000">'</font>
<font color="#FF0000">declare</font>
<font color="#FF0000">        output record;</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        select $1,$2,$3 into output;</font>
<font color="#FF0000">        return output;</font>
<font color="#FF0000">end;</font>
<font color="#FF0000">'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from examplefunc(1,2,3) as (col1 int, col2 float, col3 char(20))"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">4</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out1<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out2<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out3<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
</tt></pre>

</blockquote>
<h4>MySQL/MariaDB</h4>

<p>Here's how you can get multiple values from the result
set of a MySQL/MariaDB procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> begin <font color="#008080">select</font> in1<font color="#990000">,</font> in2<font color="#990000">,</font> in3<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"call exampleproc(?,?,?)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindLong</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindDouble</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#993399">4</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_inputBindString</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out1<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out2<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out3<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<p>Here's how you can get multiple values from the output variables
of a MySQL/MariaDB procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">(</font>out <font color="#008080">out1</font> <font color="#009900">int</font><font color="#990000">,</font> out <font color="#008080">out2</font> <font color="#009900">float</font><font color="#990000">,</font> out <font color="#008080">out3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> begin select <font color="#993399">1</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'hello'</font> <font color="#008080">into</font> out1<font color="#990000">,</font> out2<font color="#990000">,</font> out3<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"set @out1=0, @out2=0.0, @out3=''"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"call exampleproc(@out1,@out3,@out3)"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select @out1,@out2,@out3"</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out1<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out2<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>out3<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h3>Result Sets</h3>

<p>Some stored procedures return entire result sets.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create or replace package types as
        type cursorType is <font color="#008080">ref</font> cursor<font color="#990000">;</font>
end<font color="#990000">;</font>

create function exampleproc <font color="#008080">return</font> types<font color="#990000">.</font>cursortype is
        <font color="#008080">l_cursor</font>    types<font color="#990000">.</font>cursorType<font color="#990000">;</font>
begin
        <font color="#008080">open</font> l_cursor <font color="#008080">for</font> select <font color="#990000">*</font> <font color="#008080">from</font> mytable<font color="#990000">;</font>
        <b><font color="#0000FF">return</font></b> l_cursor<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_prepareQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"begin  :curs:=exampleproc; end;"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_defineOutputBindCursor</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"curs"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_executeQuery</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
<font color="#008080">sqlrcur</font> bindcur<font color="#990000">=</font><b><font color="#000000">sqlrcur_getOutputBindCursor</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"curs"</font><font color="#990000">);</font>
<b><font color="#000000">sqlrcur_fetchFromBindCursor</font></b><font color="#990000">(</font>bindcur<font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field00<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>bindcur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field01<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>bindcur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field02<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>bindcur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field10<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>bindcur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field11<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>bindcur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field12<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>bindcur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop function exampleproc
drop package types
</tt></pre>

</blockquote>
<br/><h4>Sybase and Microsoft SQL Server</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create procedure exampleproc <font color="#008080">as</font> select <font color="#990000">*</font> from exampletable
</tt></pre>

</blockquote>
<p>To exceute the stored procedure from an SQL Relay program, ue code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"exec exampleproc"</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field00<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field01<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field02<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field10<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field11<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field12<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><h4>Firebird</h4>

<p>Stored procedures in Firebird can return a result set if a
select query in the procedure selects values into the output parameters and
then issues a suspend command, however SQL Relay doesn't currently support
stored procedures that return result sets.</p>

<h4>DB2</h4>

<p>Stored procedures in DB2 can return a result set if the procedure is declared
to return one, however SQL Relay doesn't currently support stored procedures
that return result sets.</p>

<h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">examplefunc</font></b><font color="#990000">()</font> returns setof <font color="#008080">record</font> as <font color="#FF0000">'</font>
<font color="#FF0000">        declare output record;</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        for output in select * from mytable loop</font>
<font color="#FF0000">                return next output;</font>
<font color="#FF0000">        end loop;</font>
<font color="#FF0000">        return;</font>
<font color="#FF0000">end;</font>
<font color="#FF0000">'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from examplefunc() as (exampleint int, examplefloat float, examplechar char(40))"</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field00<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field01<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field02<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field10<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field11<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field12<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop function examplefunc
</tt></pre>

</blockquote>
<h4>MySQL/MariaDB</h4>

<p>The result sets of all select statements called within MySQL/MariaDB stored
procedures (that aren't selected into variables) are returned from the procedure
call.  Though MySQL/MariaDB stored procedures can return multiple result sets,
currently SQL Relay can only fetch the first result set.</p>

<p>To create the stored procedure which returns a result set, run a query like
the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">exampleproc</font></b><font color="#990000">()</font> <font color="#008080">begin</font> select <font color="#990000">*</font> <font color="#008080">from</font> mytable<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"call exampleproc()"</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field00<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field01<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field02<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field10<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field11<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#009900">char</font>    <font color="#990000">*</font>field12<font color="#990000">=</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">1</font><font color="#990000">,</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure exampleproc
</tt></pre>

</blockquote>
<br/><a name="caching"/><h2>Caching The Result Set</h2>

<p>Say you're writing a web-based report where a query with a huge result set
is executed and 20 rows are displayed per page.  Rather than rerunning the 
query for every page every time and dumping all but the 20 rows you want to
display, you can run the query once, cache the result set to a local file and
just open the file for each page of the report.</p>

<p>First page:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#009900">char</font>       <font color="#990000">*</font>filename<font color="#990000">;</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

	<font color="#990000">...</font> generate a unique <font color="#008080">file</font> name <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_cacheToFile</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>filename<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_setCacheTtl</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#993399">600</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"select * from my_table"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_cacheOff</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>

        <font color="#990000">...</font> pass the filename to the <font color="#008080">next</font> page <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>Second page:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#009900">int</font>        row<font color="#990000">,</font>col<font color="#990000">;</font>

        <font color="#990000">...</font> get the filename from the <font color="#008080">previous</font> page <font color="#990000">...</font>

        <font color="#990000">...</font> get the page to display from the <font color="#008080">previous</font> page <font color="#990000">...</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_openCachedResultSet</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>filename<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>row<font color="#990000">=</font>pagetodisplay<font color="#990000">*</font><font color="#993399">20</font><font color="#990000">;</font> row<font color="#990000">&lt;(</font>pagetodisplay<font color="#990000">+</font><font color="#993399">1</font><font color="#990000">)*</font><font color="#993399">20</font><font color="#990000">;</font> row<font color="#990000">++)</font> <font color="#FF0000">{</font>
                <b><font color="#0000FF">for</font></b> <font color="#990000">(</font>col<font color="#990000">=</font><font color="#993399">0</font><font color="#990000">;</font> col<font color="#990000">&lt;</font><b><font color="#000000">sqlrcur_colCount</font></b><font color="#990000">(</font>cur<font color="#990000">);</font> col<font color="#990000">++)</font> <font color="#FF0000">{</font>
                        <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"%s,"</font><font color="#990000">,</font><b><font color="#000000">sqlrcur_getFieldByIndex</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>row<font color="#990000">,</font>col<font color="#990000">));</font>
                <font color="#FF0000">}</font>
                <b><font color="#000000">printf</font></b><font color="#990000">(</font><font color="#FF0000">"</font><font color="#CC33CC">\n</font><font color="#FF0000">"</font><font color="#990000">);</font>
        <font color="#FF0000">}</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>For result-set caching to be effective, the sqlr-cachemanager process must be enabled and running on the client system.  The sqlr-cachemanager scans the cache periodically and deletes cached result sets whos ttl's have expired.  If it is not running, stale result sets persist beyond their intended ttl's.</p>

<p>To enable the sqlr-cachemanager at boot on systemd platforms:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>systemctl <font color="#008080">enable</font> sqlrcachemanager<font color="#990000">.</font>service</tt></pre>

</blockquote>
<p>To start the sqlr-cachemanager on systemd platforms:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>systemctl <font color="#008080">start</font> sqlrcachemanager<font color="#990000">.</font>service</tt></pre>

</blockquote>
<p>To enable the sqlr-cachemanager at boot on most non-systemd platforms, you must create a symlink into the /etc/rc2.d or /etc/rc3.d directory.  Eg:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cd <font color="#990000">/</font>etc<font color="#990000">/</font>rc2<font color="#990000">.</font>d
ln <font color="#990000">-</font>s <font color="#990000">../</font>init<font color="#990000">.</font>d<font color="#990000">/</font>sqlrcachemanager S15sqlrcachemanager</tt></pre>

  <p>or</p>

<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>cd <font color="#990000">/</font>etc<font color="#990000">/</font>rc3<font color="#990000">.</font>d
ln <font color="#990000">-</font>s <font color="#990000">../</font>init<font color="#990000">.</font>d<font color="#990000">/</font>sqlrcachemanager S15sqlrcachemanager</tt></pre>

</blockquote>
<p>To start the sqlr-cachemanager on most non-systemd platforms:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#990000">/</font>etc<font color="#990000">/</font>init<font color="#990000">.</font>d<font color="#990000">/</font>sqlrcachemanager start</tt></pre>

</blockquote>
<p>To enable the sqlr-cachemanager at boot on FreeBSD platforms, edit /etc/rc.conf and add a line like:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>sqlrcachemanager_enable<font color="#990000">=</font>YES</tt></pre>

</blockquote>
<p>To enable the sqlr-cachemanager at boot on NetBSD platforms, edit /etc/rc.conf and add a line like:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>sqlrcachemanager<font color="#990000">=</font>YES</tt></pre>

</blockquote>
<p>To enable the sqlr-cachemanager at boot on OpenBSD platforms, edit /etc/rc.conf and add a line like:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>sqlrcachemanager_flags<font color="#990000">=</font>YES</tt></pre>

</blockquote>
<p>To start the sqlr-cachemanager on BSD platforms:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#990000">/</font>etc<font color="#990000">/</font>init<font color="#990000">.</font>d<font color="#990000">/</font>sqlrcachemanager start</tt></pre>

</blockquote>
<br/><a name="suspending"/><h2>Suspending and Resuming Sessions</h2>

<p>Sometimes web-based applications need a single database transaction to span
multiple pages.  Since SQL Relay sessions can be suspended and resumed, this
is possible.</p>

<p>First page:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#009900">int</font>        port<font color="#990000">;</font>
        <font color="#009900">char</font>       <font color="#990000">*</font>socket<font color="#990000">;</font>
        <font color="#009900">int</font>        rs<font color="#990000">;</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"insert into my_table values (1,2,3)"</font><font color="#990000">);</font>
	<b><font color="#000000">sqlrcur_suspendResultSet</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_suspendSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
	rs<font color="#990000">=</font><b><font color="#000000">sqlrcur_getResultSetId</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        port<font color="#990000">=</font><b><font color="#000000">getConnectionPort</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        socket<font color="#990000">=</font><b><font color="#000000">getConnectionSocket</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>

        <font color="#990000">...</font> pass <font color="#008080">the</font> rs<font color="#990000">,</font> port and socket to the <font color="#008080">next</font> page <font color="#990000">...</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>Second page:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;sqlrelay/sqlrclientwrapper.h&gt;</font>
<b><font color="#000080">#include</font></b> <font color="#FF0000">&lt;stdio.h&gt;</font>

<b><font color="#000000">main</font></b><font color="#990000">()</font> <font color="#FF0000">{</font>

        <font color="#990000">...</font> <font color="#008080">get</font> rs<font color="#990000">,</font> port and socket from <font color="#008080">previous</font> page <font color="#990000">...</font>

        <font color="#008080">sqlrcon</font>      con<font color="#990000">=</font><b><font color="#000000">sqlrcon_alloc</font></b><font color="#990000">(</font><font color="#FF0000">"sqlrserver"</font><font color="#990000">,</font><font color="#993399">9000</font><font color="#990000">,</font><font color="#FF0000">"/tmp/example.socket"</font><font color="#990000">,</font><font color="#FF0000">"user"</font><font color="#990000">,</font><font color="#FF0000">"password"</font><font color="#990000">,</font><font color="#993399">0</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
        <font color="#008080">sqlrcur</font>      cur<font color="#990000">=</font><b><font color="#000000">sqlrcur_alloc</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcon_resumeSession</font></b><font color="#990000">(</font>con<font color="#990000">,</font>port<font color="#990000">,</font>socket<font color="#990000">);</font>
	<b><font color="#000000">sqlrcur_resumeResultSet</font></b><font color="#990000">(</font>cur<font color="#990000">,</font>rs<font color="#990000">);</font>
        <b><font color="#000000">sqlrcur_sendQuery</font></b><font color="#990000">(</font>cur<font color="#990000">,</font><font color="#FF0000">"commit"</font><font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_endSession</font></b><font color="#990000">(</font>con<font color="#990000">);</font>

        <b><font color="#000000">sqlrcur_free</font></b><font color="#990000">(</font>cur<font color="#990000">);</font>
        <b><font color="#000000">sqlrcon_free</font></b><font color="#990000">(</font>con<font color="#990000">);</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>You can also distribute the processing of a result set across a series of
pages using suspended sessions.  If you're buffering a result set in chunks
instead of all at once and suspend a session, when you resume the session you 
can continue to retrieve rows from the result set.</p>

<p>Similarly, if you're buffering a result set in chunks, caching that
result set and suspend your session.  When you resume the session, you can
continue caching the result set.  You must use sqlrcur_resumeCachedResultSet()
instead of sqlrcur_resumeResultSet() however.</p>

<a name="lastinsertid"/><h2>Getting the Last Insert ID</h2>

<p>Databases with autoincrement or identity columns often provide functions which return the "last insert id"; the value of the autoincrement column that was generated during the insert into the database.</p>

<p>SQL Relay provides the sqlrcon_getLastInsertId() function to get this value.</p>

<p>When using the SQLite database, you can also get the last insert id by running the query:</p>

<blockquote>
<b>select last insert rowid</b>
</blockquote>
</body>
</html>
